package config

import (
	"github.com/joho/godotenv"
	log "github.com/sirupsen/logrus"
	"os"
	"strconv"
)

var (
	PGHost   string
	PGPort   string
	PGUser   string
	PGPasswd string
	PGDb     string

	MinioEndpoint  string
	MinioAccessKey string
	MinioSecretKey string
	MinioBucket    string
	MinioUseSSL    bool

	AIUrl string

	JwtEnabled bool
)

func InitEnv() {
	if err := godotenv.Load(); err != nil {
		log.Warning(".env file not found")
	}
	if os.Getenv("PG_HOST") == "" || os.Getenv("PG_PORT") == "" || os.Getenv("PG_USER") == "" || os.Getenv("PG_PASSWD") == "" || os.Getenv("PG_DB") == "" {
		panic("pg db相关环境变量未设置")
	}

	if os.Getenv("MINIO_ENDPOINT") == "" || os.Getenv("MINIO_ACCESSKEY") == "" || os.Getenv("MINIO_SECRETKEY") == "" || os.Getenv("MINIO_BUCKET") == "" || os.Getenv("MINIO_USESSL") == "" {
		panic("minio 相关环境变量未设置")
	}

	if os.Getenv("AI_URL") == "" {
		panic("Ai 相关环境变量未设置")
	}

	PGHost = os.Getenv("PG_HOST")
	PGPort = os.Getenv("PG_PORT")
	PGUser = os.Getenv("PG_USER")
	PGPasswd = os.Getenv("PG_PASSWD")
	PGDb = os.Getenv("PG_DB")

	MinioEndpoint = os.Getenv("MINIO_ENDPOINT")
	MinioAccessKey = os.Getenv("MINIO_ACCESSKEY")
	MinioSecretKey = os.Getenv("MINIO_SECRETKEY")
	MinioBucket = os.Getenv("MINIO_BUCKET")
	MinioUseSSL, _ = strconv.ParseBool(os.Getenv("MINIO_USESSL"))

	AIUrl = os.Getenv("AI_URL")

	jwtEnabledStr := os.Getenv("JWT_ENABLED")
	if jwtEnabledStr == "" {
		jwtEnabledStr = "true"
	}
	jwtEnabled, err := strconv.ParseBool(jwtEnabledStr)
	if err != nil {
		log.Fatal("JWT_ENABLED 变量设置不正确")
	}
	JwtEnabled = jwtEnabled
}
